import {
    createRouter,
    createWebHistory
} from 'vue-router'

import { WEBSITE_TITLE } from '@/config/env'; 
const routes = [{
    path: '/',
    redirect: '/login'
},
{
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
},
{
    path: '/',
    name: 'Main',
    redirect: '/home',
    component: () => import('../views/Main.vue'),
    children: [
        {
            path: '/code',
            name: 'code',
            component: () => import('../views/code.vue'),
        }, 
        {
            path: '/branch',
            name: 'branch',
            component: () => import('../views/branch.vue'),
        }, 
        {
            path: '/setting',
            name: 'setting',
            component: () => import('../views/setting.vue'),
        }, 
        {
            path: '/store',
            name: 'store',
            component: () => import('../views/store.vue'),
        }, 
        {
            path: '/station',
            name: 'station',
            component: () => import('../views/station.vue'),
        }, 
        {
            path: '/banner',
            name: 'banner',
            component: () => import('../views/banner.vue'),
        }, 
        {
            path: '/notice',
            name: 'notice',
            component: () => import('../views/notice.vue'),
        }, 
        {
            path: '/form',
            name: 'form',
            component: () => import('../views/form.vue'),
        }, 
        {
            path: '/manager',
            name: 'Manager',
            component: () => import('../views/Manager/Manager.vue'),
        }, {
            path: '/addManager',
            name: 'AddManager',
            component: () => import('../views/Manager/Add.vue'),
        }, {
            path: '/editManager/:id',
            name: 'EditManager',
            component: () => import('../views/Manager/Edit.vue'),
        },{
            path: '/user',
            name: 'User',
            component: () => import('../views/User.vue'),
        },{
            path: '/car',
            name: 'car',
            component: () => import('../views/car.vue'),
        },
        {
            path: '/xchange_order',
            name: 'xchange_order',
            component: () => import('../views/exchange_order.vue'),
        },
        {
            path: '/blance',
            name: 'blance',
            component: () => import('../views/blance.vue'),
        },
        {
            path: '/hotel',
            name: 'hotel',
            component: () => import('../views/hotel.vue'),
        },
        {
            path: '/hotel_add',
            name: 'hotel_add',
            component: () => import('../views/hotel_add.vue'),
        },
        {
            path: '/hall',
            name: 'hall',
            component: () => import('../views/hall.vue'),
        },
        {
            path: '/hall_add',
            name: 'hall_add',
            component: () => import('../views/hall_add.vue'),
        },
        {
            path: '/phone_log',
            name: 'phone_log',
            component: () => import('../views/phone_log.vue'),
        },
        {
            path: '/information',
            name: 'information',
            component: () => import('../views/information.vue'),
        },
        {
            path: '/information_add',
            name: 'information_add',
            component: () => import('../views/information_add.vue'),
        },
        {
            path: '/invite',
            name: 'invite',
            component: () => import('../views/invite.vue'),
        },
        {
            path: '/distinguish_log',
            name: 'distinguish_log',
            component: () => import('../views/distinguish_log.vue'),
        },
        {
            path: '/bride',
            name: 'bride',
            component: () => import('../views/bride.vue'),
        },

        // demo
        // {
        //     path: '/gzry',
        //     name: 'gzry',
        //     component: () => import('../views/cankao/gzry.vue'),
        // }, {
        //     path: '/notify_bind',
        //     name: 'notify_bind',
        //     component: () => import('../views/cankao/notify_bind.vue'),
        // }, {
        //     path: '/information',
        //     name: 'information',
        //     component: () => import('../views/cankao/information.vue'),
        // }, 
    ]
}, {
    path: '/404',
    name: '404',
    component: () => import('../views/404.vue')
},
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

router.beforeEach((to, from, next) => {
    document.title = WEBSITE_TITLE
    const token = sessionStorage.getItem('token')
    if (to.path == "/login") {
        if (!token) {
            return next()
        } else {
            return next('/hotel')
        }
    } else {
        if (!token) {
            return next('/login')
        } else {
            if (to.matched.length === 0) {
                next('/hotel')
            }
            next()
        }
    }
})

export default router